package tc.orcl.lab4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;

public class Task5
{
	public static void main(String[] args)
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("JDBC driver is OK.");
		}
		catch (ClassNotFoundException e)
		{
			System.out.println("Error: JDBC driver is wrong!");
			System.out.println(e.getMessage());
			e.printStackTrace();
			System.exit(0);
		}
			
		Locale locale = Locale.getDefault();
		System.out.println("Default locale is " + locale);
		Locale.setDefault(Locale.ENGLISH);
			
		String url = new String("");
		url = "jdbc:oracle:thin:@localhost:1521:XE";
			
		String user = "vadik3";
		String password  = "oxe";
		
		try
		{
			Connection con = DriverManager.getConnection(url, user, password);
			System.out.println("Connection with URL " + url + " is OK.");
			
			Locale.setDefault(locale);
			
			try
			{				
				Statement st = con.createStatement();
				con.setAutoCommit(false);
				
//				String sql = "CREATE TABLE photos "
//							+ "( "
//								+ "employee_id NUMBER(6), "
//								+ "photo BLOB, "
//								+ "CONSTRAINT photos_pkey "
//									+ "PRIMARY KEY (employee_id), "
//								+ "CONSTRAINT null_photo "
//									+ "CHECK (photo IS NOT NULL) "
//							+ ")";
//				st.executeUpdate(sql);
				
				System.out.println("CREATE is OK.");
				
				st.close();
			}
			catch (SQLException e)
			{
				System.out.println("Error: CREATE is wrong!");
				System.out.println(e.getMessage());
			}
			
			String currentDir = System.getProperty("user.dir") + "\\src\\orcl\\lab4\\";
			
			try
			{				
				Statement st = con.createStatement();
				con.setAutoCommit(false);
				
				FileInputStream fis1 = null, fis2 = null;
				byte[][] photos = {null, null};
				try
				{
					fis1 = new FileInputStream(currentDir + "pic1.jpg");
					photos[0] = new byte[fis1.available()];
					fis1.read(photos[0]);
					
					fis2 = new FileInputStream(currentDir + "pic2.jpg");
					photos[1] = new byte[fis2.available()];
					fis2.read(photos[1]);
				}
				catch (FileNotFoundException e)
				{
					e.printStackTrace();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				finally
				{
					try{fis1.close();fis2.close();} catch (IOException e){e.printStackTrace();}
				}

				System.out.printf("%d", photos[0]);
				
//				String sql = "INSERT INTO photos "
//						   + "VALUES ()"
//						+ "( "
//							+ "employee_id NUMBER(6), "
//							+ "photo BLOB, "
//							+ "CONSTRAINT photos_pkey "
//								+ "PRIMARY KEY (employee_id), "
//							+ "CONSTRAINT null_photo "
//								+ "CHECK (photo IS NOT NULL) "
//						+ ")";
//			st.executeUpdate(sql);
				
				System.out.println("INSERT is OK.");
				
				st.close();
			}
			catch (SQLException e)
			{
				System.out.println("Error: INSERT is wrong!");
				System.out.println(e.getMessage());
			}
			
			con.commit();
			con.close();
		}		
		catch (SQLException e)
		{
			System.out.println("Error: Connection is wrong!");
			e.printStackTrace();
		}
	}
	
}
